Imports System.Runtime.Serialization
Imports System.Runtime.Versioning
Imports BikiSoft.BikSettings

<Serializable(), ObfuscationAttribute(Exclude:=True, ApplyToMembers:=True)> Friend Class SubConfig

    Private m_tables As TableCont = Nothing
    Private m_vars As New Vars("Variables")
    <OptionalField(VersionAdded:=3)> _
    Private m_Bvars As BVars = Nothing
    <OptionalField(VersionAdded:=3)> _
    Private m_BVarRef As String = ""

    Private m_name As String = ""
    Private m_description As BikDescription = Nothing

    Private m_next As SubConfig = Nothing

    <OptionalField(VersionAdded:=2)> _
    Private m_id As BikSettings.BK_ioID = BikSettings.BK_ioID.BK_IO_NONE
    <OptionalField(VersionAdded:=2)> _
    Private m_Num As Integer = 0
    <OptionalField(VersionAdded:=2)> _
    Private m_Offset As ULong

    Sub New(Optional ByVal Name As String = "")
        m_name = Name
        m_tables = New TableCont("Tables") : m_vars = New Vars("Parameters")
    End Sub
    Sub New(ByRef Array As Byte(), ByVal Offset As UInt16, ByVal ID As BikSettings.BK_ioID, ByVal Num As Byte, ByVal Vars As BVars)
        Me.New()
        Me.ID = ID
        m_Num = Num
        m_Offset = Offset
        create(Array, Offset, ID, Num, Vars)
        m_Bvars = Vars
    End Sub
    Friend Sub addNode(ByRef Node As SubConfig)
        If Not IsNothing(Node) Then If IsNothing(m_next) Then m_next = Node Else m_next.addNode(Node)
    End Sub
    Private Sub create(ByRef Array As Byte(), ByVal Offset As UInt16, ByVal ID As BikSettings.BK_ioID, ByVal Num As Byte, ByRef Vars As BVars)
        Dim f_table As Table
        Select Case ID
            Case BikSettings.BK_ioID.BK_CONSULT
                m_name = "Data Stream"
                m_vars.addNode(New Var("RPM", New BikDescription("RPM stream variable"), &H681, 1.0F / 3.0F, 8, &H3))
                m_vars.addNode(New Var("MAF", New BikDescription("MAF stream variable"), &H681, 1.0F / 48.0F, 8, &H30))
                m_vars.addNode(New Var("Injectors", New BikDescription("Injector stream variable"), &H67F, 1.0F / 48.0F, 8, &H30))
                m_vars.addNode(New Var("Coolant", New BikDescription("Coolant Temp stream variable"), &H680, 1, 1, 0, 0, False))
                m_vars.addNode(New Var("o2 Sensor", New BikDescription("Narrow Band o2 stream var"), &H680, 1, 1, 0, 1, False))
                m_vars.addNode(New Var("Speed", New BikDescription("Speed stream variable"), &H680, 1, 1, 0, 2, False))
                m_vars.addNode(New Var("BVolts", New BikDescription("Battery Voltage stream var"), &H680, 1, 1, 0, 3, False))
                m_vars.addNode(New Var("Load", New BikDescription("Live Load Trace var"), &H680, 1, 1, 0, 7, False))
                m_vars.addNode(New Var("Multi", New BikDescription("ECU Multi var"), &H67F, 1, 1, 0, 0, False))
                m_vars.addNode(New Var("Ve", New BikDescription("Live Ve Trace var"), &H67F, 1, 1, 0, 1, False))
                m_vars.addNode(New Var("Ignition", New BikDescription("Ignition Timing stream var"), &H67F, 1, 1, 0, 6, False))
                m_vars.addNode(New Var("AAC", New BikDescription("Idle Air stream var"), &H67F, 1, 1, 0, 7, False))
                m_vars.addNode(New Var("TPS", New BikDescription("TPS stream variable"), &H680, 1, 1, 0, 5, False))
                m_vars.addNode(New Var("DBR1", New BikDescription("Bit Register 1 var"), &H67F, 1, 1, 0, 3, False))
                m_vars.addNode(New Var("DBR2", New BikDescription("Bit Register 2 var"), &H67E, 1, 1, 0, 6, False))
                m_vars.addNode(New Var("DBR3", New BikDescription("Bit Register 3 var"), &H67E, 1, 1, 0, 7, False))

                Dim f_opts() As States.comboBoxItem = {New States.comboBoxItem("Stream", 0), New States.comboBoxItem("Off", 1)}
                For Each item As Var In m_vars.getAsArray
                    item.SelectionValues = f_opts
                Next
                m_tables = Nothing

            Case BikSettings.BK_ioID.BK_DAC
                Dim f_str(63) As String
                For i As Integer = 0 To 63
                    f_str(i) = Str(80 * i)
                Next
                m_BVarRef = "DAC"
                m_name = "DAC Outputs"
                m_vars = Nothing

                ' Initialise the BikiRom variable(s)
                If Not IsNothing(Vars) Then
                    If IsNothing(Vars.getByName("DAC FM1")) Then
                        Vars.addNode(New BVar("DAC FM1", "Output Formula", BVar.bvarType.BV_TXT, "", "DAC"))
                    End If

                    Vars.getByName("DAC FM1").setKey("DAC")
                    Vars.getByName("DAC FM1").Value = BikSettings.nAuFirstByte
                Else
                    Throw New Exception("Variables not specified, BikiSoft Error")
                End If

                m_tables.addTable(New Table(&HC70, 64, 1, "DAC Calibration Table", 2))
                m_tables(0).setAxis(False, False, False, DisplayMode.BK_DISP_RAW, DisplayScheme.BK_SCH_WORD)
                m_tables(0).getWidthScale.Overlay = f_str
                m_tables(0).getWidthScale.Trace = traceScheme.TRC_NONE

            Case BikSettings.BK_ioID.BK_ADLOOKUPs
                Dim f_str(63) As String
                For i As Integer = 0 To 63
                    f_str(i) = Str(80 * i)
                Next

                m_name = "ADC Inputs"
                m_vars = Nothing

                m_tables.addTable(New Table(&H690, 64, 1, "ADC1 Lookup Table", 4))
                m_tables(0).setAxis(False, False, False, DisplayMode.BK_DISP_RAW, DisplayScheme.BK_SCH_SINGLE)
                m_tables(0).getWidthScale.Overlay = f_str
                m_tables(0).getWidthScale.Trace = traceScheme.TRC_AD1

                m_tables.addTable(New Table(&H7A0, 64, 1, "ADC2 Lookup Table", 4))
                m_tables(1).setAxis(False, False, False, DisplayMode.BK_DISP_RAW, DisplayScheme.BK_SCH_SINGLE)
                m_tables(1).getWidthScale.Overlay = f_str
                m_tables(1).getWidthScale.Trace = traceScheme.TRC_AD2

                m_tables.addTable(New Table(&H8B0, 64, 1, "ADC3 Lookup Table", 4))
                m_tables(2).setAxis(False, False, False, DisplayMode.BK_DISP_RAW, DisplayScheme.BK_SCH_SINGLE)
                m_tables(2).getWidthScale.Overlay = f_str
                m_tables(2).getWidthScale.Trace = traceScheme.TRC_AD3

            Case BikSettings.BK_ioID.BK_OUT
                m_name = "AX" & Num & " "

                Select Case CType(Array(Offset), BikSettings.funcCode)
                    Case BikSettings.funcCode.BF_BOOST
                        m_name &= "(Boost)"
                    Case BikSettings.funcCode.BF_GENERAL_ON_OFF
                        m_name &= "(On/Off)"
                    Case BikSettings.funcCode.BF_GENERLAL_PWM
                        m_name &= "(PWM)"
                    Case BikSettings.funcCode.BF_TURBO_TIMER
                        m_name &= "(Turbo Timer)"
                    Case funcCode.BF_PID
                        m_name &= "(PID)"
                    Case funcCode.BF_ALU
                        m_name &= "(ALU)"
                    Case Else
                        m_name &= "(Not Set)"
                End Select

                m_vars.addNode(New Var("Config Byte", New BikDescription("Configuration Byte"), Offset, 1, 8))
                m_vars("Config Byte").b_reloadMenu = True

                If ID = BikSettings.BK_ioID.BK_OUT And (Num = 1 Or Num = 3) Then
                    m_vars.addNode(New Var("PWM Config", New BikDescription("PWM Frequency in Hz (0=Off)"), Offset + &HFC, 1, 32))
                    m_vars(0).SelectionValues = New States.comboBoxItem() _
                         {New States.comboBoxItem("Unconfigured", &HFF), _
                          New States.comboBoxItem("Boost Controler", &H10), _
                          New States.comboBoxItem("Arithmetic logic unit", &H12), _
                          New States.comboBoxItem("PID Calculation", BikSettings.funcCode.BF_PID), _
                          New States.comboBoxItem("General PWM", &H40), _
                          New States.comboBoxItem("On/Off", &H21), _
                          New States.comboBoxItem("Turbo Timer", &H30)}
                Else
                    m_vars(0).SelectionValues = New States.comboBoxItem() _
                        {New States.comboBoxItem("Unconfigured", &HFF), _
                         New States.comboBoxItem("Arithmetic logic unit", &H12), _
                         New States.comboBoxItem("PID Calculation", BikSettings.funcCode.BF_PID), _
                         New States.comboBoxItem("On/Off", &H21), _
                         New States.comboBoxItem("Turbo Timer", &H30)}
                End If

                Offset += 1

                ' --------------------------------------------------------- Configuration for PWM/on off stuff
                Select Case CType(Array(Offset - 1), BikSettings.funcCode)
                    Case funcCode.BF_TURBO_TIMER
                        m_tables = Nothing
                        Dim newVar As Var = Nothing

                        Dim f_selection(20) As States.comboBoxItem
                        f_selection(0) = New States.comboBoxItem("Unconfigured", 255)
                        For q As Integer = 1 To UBound(f_selection)
                            f_selection(q) = New States.comboBoxItem("Trigger " & q, q - 1)
                        Next

                        m_vars.addNode(New Var("Minimum Time", New BikDescription("Minimum on Time in seconds"), Offset, , 16))
                        m_vars.addNode(New Var("Maximum Time", New BikDescription("Maximum on Time in seconds"), Offset + 20, , 16))

                        For nCnt As Integer = 1 To 5
                            m_vars.addNode(New Var("Timeout " & nCnt, New BikDescription("Timeout in seconds"), Offset + (nCnt * 3) + 3, , 16))
                            newVar = New Var("Logic " & nCnt, New BikDescription("Trigger # for Timeout"), Offset + (nCnt * 3) + 2, , 8)
                            newVar.SelectionValues = f_selection
                            m_vars.addNode(newVar)
                        Next

                    Case funcCode.BF_ALU
                        ' m_name = "DAC Outputs"
                        m_BVarRef = "ALU" & Num
                        'm_vars = Nothing

                        ' Initialise the BikiRom variable(s)
                        If Not IsNothing(Vars) Then
                            If IsNothing(Vars.getByName("ALU FM" & Num)) Then
                                Vars.addNode(New BVar("ALU FM" & Num, "Output Formula", BVar.bvarType.BV_TXT, "", "ALU" & Num))
                            End If

                            Vars.getByName("ALU FM" & Num).setKey("ALU" & Num)
                            Vars.getByName("ALU FM" & Num).Value = Offset
                            ' Limits the length of the compilation
                            Vars.getByName("ALU FM" & Num).AdditionalValue = 28
                        Else
                            Throw New Exception("Variables not specified, BikiSoft Error")
                        End If
                    Case funcCode.BF_PID
                        m_BVarRef = "BKOUT" & Num
                        m_tables = Nothing

                        m_vars.addNode(New Var(BikSettings.TableVariables.BCTV_AFM, Offset))
                        m_vars.addNode(New BikiSoft.Var("Target", New BikDescription("Target Value"), Offset + 22, , CByte(32), , 0))
                        m_vars.addNode(New Var("Feedback Polarity", New BikDescription("Polarity of the trigger"), Offset + 1, 1, 1, 0, 0, False))
                        m_vars("Feedback Polarity").SelectionValues = New States.comboBoxItem() { _
                            New States.comboBoxItem("Positive", 1), _
                            New States.comboBoxItem("Negative", 0) _
                        }
                        m_vars.addNode(New BikiSoft.Var("iValue", New BikDescription("Initial value"), Offset + 34, 100, CByte(32), , 0))
                        m_vars.addNode(New BikiSoft.Var("iMax", New BikDescription("Maximum integrator value"), Offset + 2, 100, CByte(32), , 0))
                        m_vars.addNode(New BikiSoft.Var("iMin", New BikDescription("Minimum integrator value"), Offset + 6, 100, CByte(32), , 0))
                        m_vars.addNode(New BikiSoft.Var("iGain", New BikDescription("Integrator Gain"), Offset + 10, 100, CByte(32), , 0))
                        m_vars.addNode(New BikiSoft.Var("pGain", New BikDescription("Proportional Gain"), Offset + 14, 100, CByte(32), , 0))
                        m_vars.addNode(New BikiSoft.Var("dGain", New BikDescription("Differential Gain"), Offset + 18, 100, CByte(32), , 0))
                        m_vars.addNode(New BikiSoft.Var("tGain", New BikDescription("Time noise Gain"), Offset + 26, , CByte(32), , 0))
                        m_vars.addNode(New BikiSoft.Var("minTime", New BikDescription("Minimum noise time (ms)"), Offset + 30, , CByte(32), , 0))

                        '' Initialise the BikiRom variable(s)
                        'If Not IsNothing(Vars) Then
                        '    If IsNothing(Vars.getByName("BKOUT" & Num)) Then
                        '        Vars.addNode(New BVar("BKOUT" & Num, "Output Formula", BVar.bvarType.BV_TXT, "", "BKOUT" & Num))
                        '    End If

                        '    Vars.getByName("BKOUT" & Num).setKey("BKOUT" & Num)
                        '    Vars.getByName("BKOUT" & Num).Value = Offset
                        'Else
                        '    Throw New Exception("Variables not specified, BikiSoft Error")
                        'End If

                    Case BikSettings.funcCode.BF_GENERAL_ON_OFF, _
                    BikSettings.funcCode.BF_BOOST, _
                    BikSettings.funcCode.BF_GENERLAL_PWM

                        For i As Integer = 0 To 8
                            f_table = New Table("fill me in later")
                            f_table.Address = -1

                            Select Case CType(Array(Offset), BikSettings.TableTypes)
                                Case BikSettings.TableTypes.BCTT_BASE
                                    f_table.Name = m_name & " - Base"
                                Case BikSettings.TableTypes.BCTT_ONOFF
                                    f_table.Name = m_name & " - OnOff"
                                Case BikSettings.TableTypes.BCTT_TRIMPER
                                    f_table.Name = m_name & " - Trim%"
                                Case BikSettings.TableTypes.BCTT_TRIMVAL
                                    f_table.Name = m_name & " - Trim"
                                Case Else
                                    f_table.Name = m_name & " - Unconfigured"
                            End Select

                            '' ----------------- Decode the Variable name, etc
                            Dim f_TableVar As BikSettings.TableVariables = CType(Array(Offset + 1), BikSettings.TableVariables)
                            Dim f_mult As Double = BikSettings.TableVariableMult(f_TableVar)
                            Dim f_Off As Integer = BikSettings.TableVariableOffset(f_TableVar)
                            Dim f_name As String = BikSettings.TableVariableName(f_TableVar)
                            If f_table.Name.EndsWith("Unconfigured") Then
                                ' do nothing
                            ElseIf f_name <> "" Then
                                f_table.Name &= " (" & f_name
                            Else
                                f_table.Name &= " (NC"
                            End If

                            Dim f_TableVars As New Vars(" - Variables")
                            '----------- Table Type
                            f_TableVars.addNode(New Var(BikSettings.TableTypes.BCTT_BASE, Offset))
                            f_TableVars(0).b_reloadMenu = True

                            '----------- Table Variable
                            f_TableVars.addNode(New Var(BikSettings.TableVariables.BCTV_AFM, CUShort(Offset + 1)))

                            f_TableVars.addNode(New Var("Table Trigger", New BikDescription("Trigger to activeate the Table"), Offset + 22, , , &H7F))
                            Dim f_selection(20) As States.comboBoxItem
                            f_selection(0) = New States.comboBoxItem("Unconfigured", 127)
                            For q As Integer = 1 To UBound(f_selection)
                                f_selection(q) = New States.comboBoxItem("Trig " & q, q - 1)
                            Next
                            f_TableVars(2).SelectionValues = f_selection
                            f_TableVars(2).b_reloadMenu = True
                            f_TableVars.addNode(New Var("Trigger Polarity", New BikDescription("Polarity of the trigger"), Offset + 22, 1, 1, 0, 7, False))
                            f_TableVars(3).SelectionValues = New States.comboBoxItem() { _
                                New States.comboBoxItem("Positive", 1), _
                                New States.comboBoxItem("Negative", 0) _
                            }
                            f_TableVars(3).b_reloadMenu = True

                            Select Case CType(Array(Offset), BikSettings.TableTypes)
                                Case BikSettings.TableTypes.BCTT_BASE, _
                                BikSettings.TableTypes.BCTT_TRIMPER, _
                                BikSettings.TableTypes.BCTT_TRIMVAL ' ------------- ADD PWM SHIT HERE

                                    If CType(Array(Offset), BikSettings.TableTypes) = BikSettings.TableTypes.BCTT_BASE Then
                                        f_table.Multiplier = CDbl(100.0F / 255.0F)
                                    Else
                                        f_table.Multiplier = CDbl(200.0F / 256.0F)
                                        f_table.Offset = -100
                                    End If
                                    f_table.Address = Offset + 6
                                    f_table.Height = 1
                                    f_table.Width = 16


                                    f_TableVars.addNode(New Var("Scale Min Value", New BikDescription("Minimum value of the scale range"), Offset + 2, f_mult, CByte(32), , f_Off))
                                    f_TableVars.addNode(New Var("Scale Max Value", New BikDescription("Maximum value of the scale range"), Offset + 23, f_mult, CByte(32), , f_Off))
                                    f_table.setWidthAxis(New Scale(f_mult, f_TableVars(4), f_TableVars(5), f_TableVar))

                                Case BikSettings.TableTypes.BCTT_ONOFF ' ---------- ADD ON/OFF shit here
                                    f_table.Address = -1
                                    f_TableVars.addNode(New Var("Trigger Type", New BikDescription("Trigger Type"), Offset + 2))
                                    f_TableVars(4).SelectionValues = New States.comboBoxItem() { _
                                        New States.comboBoxItem("Schmitt Trigger", 0), _
                                        New States.comboBoxItem("Range Value", 1) _
                                    }
                                    f_TableVars.addNode(New Var("On Value", New BikDescription("Value for the first logical operation"), Offset + 3, f_mult, CByte(32), , f_Off))
                                    f_TableVars.addNode(New Var("On Action", New BikDescription("Value One's logical operator"), Offset + 7))
                                    f_TableVars.addNode(New Var("Off Value", New BikDescription("Value for the second logical operation"), Offset + 8, f_mult, CByte(32), , f_Off))
                                    f_TableVars.addNode(New Var("Off Action", New BikDescription("Value Two's logical operator"), Offset + 12))

                                    f_TableVars(6).SelectionValues = New States.comboBoxItem() { _
                                        New States.comboBoxItem(">=", 0), _
                                        New States.comboBoxItem("<=", 1), _
                                        New States.comboBoxItem("==", 2), _
                                        New States.comboBoxItem(">", 3), _
                                        New States.comboBoxItem("<", 4) _
                                    }
                                    f_TableVars(8).SelectionValues = New States.comboBoxItem() { _
                                        New States.comboBoxItem(">=", 0), _
                                        New States.comboBoxItem("<=", 1), _
                                        New States.comboBoxItem("==", 2), _
                                        New States.comboBoxItem(">", 3), _
                                        New States.comboBoxItem("<", 4) _
                                    }
                            End Select


                            Dim f_TrigPol As Byte = f_TableVars(3).getValue(Array) ' Array(Offset + 22) And &H80
                            Dim f_Trig As Byte = f_TableVars(2).getValue(Array) 'Array(Offset + 22) And &H7F
                            If f_Trig <= 20 Then
                                f_table.Name &= ", TR" & f_Trig + 1
                                If f_TrigPol = 0 Then
                                    f_table.Name &= "-)"
                                Else
                                    f_table.Name &= ")"
                                End If
                            ElseIf Not f_table.Name.EndsWith("Unconfigured") Then
                                f_table.Name &= ")"
                            End If

                            ' Create final name
                            f_TableVars.Description = f_table.Name & f_TableVars.Description

                            f_table.TableVars = f_TableVars
                            m_tables.addTable(f_table)
skip:                       Offset += 28
                        Next
                    Case Else

                End Select

            Case BikSettings.BK_ioID.BK_TRIG
                m_name = "TR" & Num & " "

                m_vars.addNode(New Var("Map Switch", New BikDescription("Map Selection (ON)"), Offset, 1, 8))
                m_vars("Map Switch").b_reloadMenu = True
                m_vars("Map Switch").SelectionValues = New States.comboBoxItem() { _
                    New States.comboBoxItem("Unconfigured", &HFF), _
                    New States.comboBoxItem("Map 1" & CarBase.GetMapNameFormatted(Me.m_Bvars, 1), 0), _
                    New States.comboBoxItem("Map 2" & CarBase.GetMapNameFormatted(Me.m_Bvars, 2), 1), _
                    New States.comboBoxItem("Map 3" & CarBase.GetMapNameFormatted(Me.m_Bvars, 3), 2), _
                    New States.comboBoxItem("Map 4" & CarBase.GetMapNameFormatted(Me.m_Bvars, 4), 3), _
                    New States.comboBoxItem("Map 5" & CarBase.GetMapNameFormatted(Me.m_Bvars, 5), 4), _
                    New States.comboBoxItem("Map 6" & CarBase.GetMapNameFormatted(Me.m_Bvars, 6), 5), _
                    New States.comboBoxItem("Map 7" & CarBase.GetMapNameFormatted(Me.m_Bvars, 7), 6), _
                    New States.comboBoxItem("Map 8" & CarBase.GetMapNameFormatted(Me.m_Bvars, 8), 7), _
                    New States.comboBoxItem("Map 9" & CarBase.GetMapNameFormatted(Me.m_Bvars, 9), 8), _
                    New States.comboBoxItem("Map 10" & CarBase.GetMapNameFormatted(Me.m_Bvars, 10), 9), _
                    New States.comboBoxItem("Map 11" & CarBase.GetMapNameFormatted(Me.m_Bvars, 11), 10), _
                    New States.comboBoxItem("Map 12" & CarBase.GetMapNameFormatted(Me.m_Bvars, 12), 11), _
                    New States.comboBoxItem("Map 13" & CarBase.GetMapNameFormatted(Me.m_Bvars, 13), 12), _
                    New States.comboBoxItem("Map 14" & CarBase.GetMapNameFormatted(Me.m_Bvars, 14), 13), _
                    New States.comboBoxItem("Map 15" & CarBase.GetMapNameFormatted(Me.m_Bvars, 15), 14) _
                }
                m_vars.addNode(New Var("Trigger Polarity", New BikDescription("Polarity Of the trigger", "Can inverse the polarity of the Trigger, so that the ON state is OFF"), Offset + 1, 1, 1, 0, 0, False))
                m_vars("Trigger Polarity").SelectionValues = New States.comboBoxItem() { _
                    New States.comboBoxItem("Positive", 1), _
                    New States.comboBoxItem("Negative", 0) _
                }
                m_vars("Trigger Polarity").b_reloadMenu = True

                Offset += 2
                m_vars.addNode(New Var(BikSettings.TableTypes.BCTT_ONOFF, Offset))
                m_vars.addNode(New Var(BikSettings.TableVariables.BCTV_AFM, CUShort(Offset + 1)))
                Dim f_TableVar As BikSettings.TableVariables = CType(Array(Offset + 1), BikSettings.TableVariables)
                Dim f_mult As Double = BikSettings.TableVariableMult(f_TableVar)
                Dim f_Off As Integer = BikSettings.TableVariableOffset(f_TableVar)
                Dim f_name As String = BikSettings.TableVariableName(f_TableVar)
                If f_name <> "" Then
                    m_name &= " (" & f_name
                Else
                    m_name &= " (NC"
                End If

                m_vars.addNode(New Var("Trigger Type", New BikDescription("Trigger Type"), Offset + 2))
                m_vars("Trigger Type").SelectionValues = New States.comboBoxItem() { _
                    New States.comboBoxItem("Schmitt Trigger", 0), _
                    New States.comboBoxItem("Range Value", 1) _
                }
                m_vars.addNode(New Var("On Value", New BikDescription("Value for the first logical operation"), Offset + 3, f_mult, CByte(32), , f_Off))
                m_vars.addNode(New Var("On Action", New BikDescription("Value One's logical operator"), Offset + 7))
                m_vars.addNode(New Var("Off Value", New BikDescription("Value for the second logical operation"), Offset + 8, f_mult, CByte(32), , f_Off))
                m_vars.addNode(New Var("Off Action", New BikDescription("Value Two's logical operator"), Offset + 12))

                m_vars("On Action").SelectionValues = New States.comboBoxItem() { _
                    New States.comboBoxItem(">=", 0), _
                    New States.comboBoxItem("<=", 1), _
                    New States.comboBoxItem("==", 2), _
                    New States.comboBoxItem(">", 3), _
                    New States.comboBoxItem("<", 4) _
                }
                m_vars("Off Action").SelectionValues = New States.comboBoxItem() { _
                    New States.comboBoxItem(">=", 0), _
                    New States.comboBoxItem("<=", 1), _
                    New States.comboBoxItem("==", 2), _
                    New States.comboBoxItem(">", 3), _
                    New States.comboBoxItem("<", 4) _
                }

                m_vars.addNode(New Var("Logical Operator", New BikDescription("Logical operator (eg Trig1 xor Trig2)"), Offset + 13, 1.0! / 32.0!, 8, &HE0))
                m_vars.addNode(New Var("Logical Input", New BikDescription("Logical Input eg (This xor Trig##)"), Offset + 13, 1, 8, &H1F))

                m_vars("Logical Operator").SelectionValues = New States.comboBoxItem() { _
                    New States.comboBoxItem("N/A", &H7), _
                    New States.comboBoxItem("AND", &H0), _
                    New States.comboBoxItem("NAND", &H1), _
                    New States.comboBoxItem("OR", &H2), _
                    New States.comboBoxItem("NOR", &H3), _
                    New States.comboBoxItem("XOR", &H4), _
                    New States.comboBoxItem("NXOR", &H5) _
                }

                Dim f_Trigs(19) As States.comboBoxItem
                For i As Integer = 0 To 19
                    f_Trigs(i) = New States.comboBoxItem("Trig " & i + 1, i)
                Next
                m_vars("Logical Input").SelectionValues = f_Trigs

                Dim f_TrigPol As Byte = m_vars("Trigger Polarity").getValue(Array)
                If f_TrigPol = 0 Then
                    m_name &= ")-"
                Else
                    m_name &= ")"
                End If

                Dim f_SecOp As Byte = m_vars("Logical Operator").getValue(Array)
                Dim f_SecTr As Byte = m_vars("Logical Input").getValue(Array)
                If f_SecOp <> &H7 And f_SecTr <= 19 Then
                    If f_SecOp Mod 2 = 1 Then
                        m_name &= " !"
                    Else
                        m_name &= " "
                    End If
                    If f_SecOp = 1 Or f_SecOp = 0 Then
                        m_name &= "&&"
                    ElseIf f_SecOp = 3 Or f_SecOp = 2 Then
                        m_name &= "|"
                    ElseIf f_SecOp = 4 Or f_SecOp = 5 Then
                        m_name &= "^"
                    End If
                    m_name &= " (TR" & f_SecTr + 1 & ")"
                End If

                Dim f_MapSw As Byte = m_vars("Map Switch").getValue(Array)
                If f_MapSw <> &HFF Then
                    m_name &= " {Map" & f_MapSw + 1 & "}"
                End If
        End Select
    End Sub
    Public Sub Refresh(ByRef Array As Byte())
        m_tables = New TableCont("Tables")
        If Not IsNothing(m_vars) Then m_vars.Clear()
        m_BVarRef = ""
        create(Array, m_Offset, m_id, m_Num, m_Bvars)
    End Sub

    Friend Function count() As Integer
        Dim f_count As Int16 = 1
        If Not IsNothing(m_next) Then
            f_count += m_next.count
        End If
        Return f_count
    End Function

    Public Property Name() As String
        Get
            Return m_name
        End Get
        Set(ByVal value As String)
            m_name = value
        End Set
    End Property
    Public Property ID() As BK_ioID
        Get
            Return m_id
        End Get
        Set(ByVal value As BK_ioID)
            m_id = value
        End Set
    End Property
    Public Property Num() As Integer
        Get
            Return m_Num
        End Get
        Set(ByVal value As Integer)
            m_Num = value
        End Set
    End Property
    Public Property Description() As BikDescription
        Get
            Return m_description
        End Get
        Set(ByVal value As BikDescription)
            m_description = value
        End Set
    End Property
    Friend Function getNext() As SubConfig
        Return m_next
    End Function
    Friend Function getTables() As TableCont
        Return m_tables
    End Function
    Friend Function getVars() As Vars
        Return m_vars
    End Function
    Friend Function getBVars() As String
        Return m_BVarRef
    End Function
    Friend Sub setBVars(ByRef Vars As BVars)
        m_Bvars = Vars
    End Sub
    Friend Sub setTables(ByRef Tables As TableCont)
        m_tables = Tables
    End Sub
    Friend Sub setVars(ByRef Varss As Vars)
        m_vars = Varss
    End Sub

    Friend Sub setNext(ByRef SubC As SubConfig)
        m_next = SubC
    End Sub
End Class